﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebUI.Controllers
{

    public class SharedController : Controller
    {
        public ActionResult ImageResizer()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ImageResizer(string tempid)
        {
            try
            {
                System.Web.Helpers.WebImage img = string.IsNullOrEmpty(Request.Files[0].FileName) ? null : new System.Web.Helpers.WebImage(Request.Files[0].InputStream);
                if (null == img)
                {
                    return Content("<script type='text/javascript'>alert('没有上传图片。')</script>");
                }
                img.FileName = Request.Files[0].FileName;
                if (img.Height > 1900 || img.Width > 1900)
                {
                    img.Resize(1900, 1900, true, true);
                }
                else {
                    img.Resize(img.Width,img.Height, true, true);
                }
                return File(img.GetBytes("image/jpeg"), "application/x-file-to-save", System.IO.Path.GetFileNameWithoutExtension(img.FileName) + ".jpg");
            }
            catch (Exception ex)
            {
                return Content("<script type='text/javascript'>alert('处理图片时遇到错误：+'"+ex.Message.Replace("'","\\'")+"'+。');\r\n/*\r\n"+ex.ToString()+"\r\n*/</script>");
                
            }
        }

    }
}
